var options = { 
	//target:        '#taobao_result',   // target element(s) to be updated with server response
	data:	{page_no:0},
	datatype:	'json',
	beforeSubmit:  showRequest,  // pre-submit callback 
	success:       processJson  // post-submit callback 
}; 	
var page = 0;
var page_total = 0;
//var price_sel = "";
var prices = new Array();

$(function(){
    $('#myForm').submit(function() {
		page = 0;
		options.data.page_no = 1;
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
        return false; 
    }); 

	$('#myForm').button();
	$('#taobao_result').corner();
});

function showRequest(formData, jqForm, options) { 
    //var queryString = $.param(formData); 
    //alert('About to submit: \n\n' + queryString);

	return true; 
} 

function processRaw(responseText, statusText, xhr, $form)  { 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.'); 
}

function processJson(data){
    //alert(data);
	///*
    data_json = $.parseJSON(data);
    total = data_json.products_search_response.total_results;
    products = data_json.products_search_response.products.product;
	if(page==0){
		page_total = Math.floor(total/100);
		if(total%100){
			page_total += 1;
		}
		$('#taobao_result').prepend("<li>-----------------------------------</li>");		
	}
	$.each(products,function(i){
		//product = $.parseJSON(products[i])
		//$('#taobao_result').prepend("<tr><td>"+products[i].name+"</td><td class=\"price\">"+products[i].price+"</td><tr>");
		$('#taobao_result').prepend("<li>"+"<span class=\"price\">"+products[i].price+"</span>"+products[i].name+"</li>");
    })
	$("#taobao_result").selectable({
		stop: function(){
			//var result = $("#taobao_message").empty();
			prices = null;
			prices = new Array();
			//price_sel = "";
			$(".ui-selected", this).each(function(){
				//var price = $("#taobao_result li").index(this);
				//var price = $("#taobao_result li", this).text();
				var price = $(this.firstChild).text();
				//result.append(price + ",");
				//price_sel += (price + ",");
				prices.push(price);
			});
		}
	});

	page++;
	if(page==page_total){
		$('#taobao_result').prepend("<li>total "+total+"</li>");	
	}
	if(page<page_total){
		//page_no set to page+1
		options.data.page_no = page+1;
		$('#myForm').ajaxSubmit(options);
	}
	//*/
}
